package no.ntnu.fp.model1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class wellGui implements ActionListener {

	JButton checkButton, checkButton1, checkButton2;
	JButton comment;
	JButton LWS;
	JFrame frame;
	int wellId = 0;
	Well well;
	static boolean frameUp = false;
	
	public wellGui(){
		
	}
		
	private Color randomColor(){
		int i = (int) Math.round(Math.random()*3);
		Color reColor;
		if(i == 0){
			reColor = Color.red;
		}else if(i==2||i==1){
			reColor = Color.green;
		}else{
			reColor = Color.yellow;
		}
		//countColors(reColor);
		return reColor;
	}
	
	private JPanel makeColorPanel(){
		JPanel out = new JPanel(new GridLayout(0,3));
		JPanel jb = new JPanel();
		JPanel jb1 = new JPanel();
		JPanel jb2 = new JPanel();
		JPanel jb3 = new JPanel();
		JPanel jb4 = new JPanel();
		JPanel js = new JPanel();
		Color rColor = randomColor();
		jb.setBackground(rColor);
		js.setBackground(randomColor());
		jb1.setBackground(jb.getBackground());
		jb2.setBackground(jb.getBackground());
		jb3.setBackground(jb.getBackground());
		jb4.setBackground(jb.getBackground());

		Border blackline = BorderFactory.createLineBorder(Color.black);
		out.setBorder(blackline);
		js.setBorder(blackline);
		
		out.add(jb);
		out.add(jb1);
		out.add(js);
		out.add(jb2);
		out.add(jb3);
		out.add(jb4);
		
		
		return out;
	}

	private JPanel addPanel(String name, JButton b){
		TitledBorder title;
		JPanel panel = new JPanel(new GridLayout(0, 1));
		title = BorderFactory.createTitledBorder(name);
		panel.setBorder(title);
		
		JPanel j = makeColorPanel();
		//b = new JButton("Check");
		//b.addActionListener(this);
		panel.add(j);
		//panel.add(b);
		return panel;
	}
	
	private JTextField makeTextField(String s){
		//JTextPane
		String s2 = " "+Math.round(Math.random()*9)+" enheter";
		JTextField out = new JTextField(s+s2, 10); //+" "+Math.round(Math.random()*9)+" enheter", 30);
		//out.setEditable(false);
		//out.set
		return out;
	}
	
	private JPanel createDataPanel(){
		JPanel out = new JPanel();
		JPanel o2 = new JPanel(new GridLayout(0, 1));
		o2.add(makeTextField("Data 1: "), BorderLayout.LINE_START);
		o2.add(makeTextField("Data 2: "), BorderLayout.LINE_START);
		o2.add(makeTextField("Data 3: "), BorderLayout.LINE_START);
		o2.add(makeTextField("Data 4: "), BorderLayout.LINE_START);
		
		out.add(o2, BorderLayout.LINE_START);
		
		return out;
	}

	protected void createGui(){
		frame = new JFrame("WellGui");
		frame.setResizable(false);
		//frame.setUndecorated(true);
		//frame.setLocationByPlatform(true);
		//frame.setLocationRelativeTo(null);
		frame.setLocation(700, 80);
		frame.setAlwaysOnTop(true);
		JPanel panel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Well "+ wellId);
		panel.setBorder(title);
		
		JPanel subP = new JPanel(new GridLayout(0, 2));
		
		
		subP.add(addPanel("This well", checkButton1));
		subP.add(createDataPanel());
		
		//subP.add(addPanel("Well2", checkButton2));
		
		//JTextArea jtp = new JTextArea("Noe text"); //Math.round((Math.random()*12))
		//pInfo.add(jtp);
		//panel.add(pInfo, BorderLayout.NORTH);
		
		JPanel sub2p = new JPanel();
		comment = new JButton("Comment");
		comment.addActionListener(this);
		LWS = new JButton("Close");
		LWS.addActionListener(this);
		sub2p.add(LWS);
		sub2p.add(comment);

		panel.add(subP, BorderLayout.CENTER);
		panel.add(sub2p, BorderLayout.PAGE_END);
		
		frame.setContentPane(panel);
				
		frame.pack();
		frame.setSize(350, 250);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public static void main(String[] args){
		new wellGui().createGui();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		//System.out.println("You pushed a button");
		int nr = 0;
		//System.out.println(e);
		if(e.getSource()==LWS){
			if(!frameUp){
				lwsGui.setFrameUp(false);
				frame.hide();
			}else{System.out.println("Please close the comment-window before closing the well-gui.");}
		}else if(e.getSource()==checkButton){
			nr = 1;
		}else if(e.getSource()==checkButton2){
			nr = 2;
		}else if(e.getSource()==comment){
			System.out.println("Opening comment gui..");
			setFrameUp(true);
			new Comment().createGui();
		}
		//System.out.println("nr "+nr);
	}
	
	public static void setFrameUp(boolean b) {
		frameUp = b;
		if(!b){
			System.out.println("Well-frame closed.");
		}
	}
}
